package com.dnjn.mes.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.mes.biz.domain.MesRecordCapacity;
import com.dnjn.mes.biz.domain.MesWorkforceShift;
import com.dnjn.mes.biz.mapper.MesRecordCapacityMapper;
import com.dnjn.mes.biz.service.IMesRecordCapacityService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 产能表 服务实现类
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@Service
public class MesRecordCapacityServiceImpl extends ServiceImpl<MesRecordCapacityMapper, MesRecordCapacity> implements IMesRecordCapacityService {

    @Override
    public MesRecordCapacity lastCapacityRecord(Long equipmentId) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(MesRecordCapacity.class)
                        .eq(MesRecordCapacity::getEquipmentId,equipmentId)
                .orderByDesc(MesRecordCapacity::getCreateTime)
                .last("limit 1")
        );
    }
    @Override
    public List<MesRecordCapacity> getEarlyRecordCapacityChart(Long equipmentId) {
        return baseMapper.getEarlyRecordCapacityChart(equipmentId);
    }

    @Override
    public List<MesRecordCapacity> getNightRecordCapacityChart(Long equipmentId) {
        return baseMapper.getNightRecordCapacityChart(equipmentId);
    }

    @Override
    public Integer getUtilizationRate(Integer defaultBeat,Long equipmentId) {
        GregorianCalendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if(hour == 0){
            hour = 23;
        }else {
            hour = hour - 1;
        }
        Integer num = baseMapper.getLastHourData(hour,equipmentId);
        //如果为空 -？
        if(Objects.isNull(num) || num <0){
            return 0;
        }
        //计算稼动率百分比
        double numD = new BigDecimal((float) num / defaultBeat).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue() * 100;
        return (int) Math.round(numD);
    }

    @Override
    public int sumTodayRecordCapacity(Long equipmentId) {

        return baseMapper.sumTodayRecordCapacity(equipmentId);
    }

    @Override
    public List<MesRecordCapacity> getEarlyTrendsCapacityByTime(long equipmentId, LocalTime startTime, LocalTime endTime,String startLocalDateTime, String endLocalDateTime) {

        return baseMapper.getEarlyTrendsCapacityByTime(equipmentId,startTime,endTime,startLocalDateTime,endLocalDateTime);
    }

    @Override
    public List<MesRecordCapacity> countRecordCapacitys(String startTime, String endTime, long equipmentId) {
        return baseMapper.countRecordCapacitys(startTime,endTime,equipmentId);
    }
}
